18000 REM ============================
18001 REM Template processing
18002 REM 
18003 REM INPUT - TMPLATE$ File to open
18004 REM         LBL$ Label array
18005 REM         REPL$ Replacements
18006 REM         ILBL Label array
18007 REM              length
18008 REM         OUTFILE$ File to write
18009 REM ============================
18010 OPEN TMPLATE$ FOR INPUT AS #3
18020 OPEN OUTFILE$ FOR OUTPUT AS #4
18030 WHILE NOT EOF(3)
18040     LINE INPUT#3, L$
18050     GOSUB 18100
18060     PRINT#4, L$
18070 WEND
18080 CLOSE #3
18085 CLOSE #4
18090 RETURN
18100 REM Search for key
18102 ILSTART = 1
18105 IF LEN(L$) < 7 THEN RETURN
18110 FOR IL = ILSTART TO LEN(L$)-2
18120     IF MID$(L$, IL, 3) = "{% " THEN GOSUB 18150:ILSTART=IL+1:GOTO 18105
18130 NEXT IL
18140 RETURN
18150 REM Find the end of the string
18160 FOR IJ = IL TO LEN(L$)-2
18170     IF MID$(L$, IJ, 3) = " %}" THEN GOTO 18200
18180 NEXT IJ
18190 RETURN
18200 REM Attempt replacement
18210 FOUNDLBL$ = MID$(L$, IL+3, IJ-(IL+3))
18220 FOR LBLINDEX = 1 TO ILBL
18230     IF LBL$[LBLINDEX] = FOUNDLBL$ THEN GOTO 18260
18240 NEXT LBLINDEX
18250 RETURN
18260 LNEW$ = LEFT$(L$, IL-1) + REPL$[LBLINDEX] + RIGHT$(L$, LEN(L$)-IJ-2)
18270 L$ = LNEW$
18280 RETURN
